﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using ProgramMain.Layers;

namespace ProgramMain.NetVis
{
    public class DiamondRange : RangeBase
    {
        public DiamondRange(Layers.VisLayer vis)
            : base(vis)
        {
            ColorRange = Brushes.Yellow;
        }

        public LineConnectionRange Ovner { get; set; }

        private const int h = 4;

        public Point VirtualLeft// основная точка, псевда начало координат
        {
            get { return VirtualPoint + new Size((-1) * h, 0); }
        }

        public Point VirtualTop { get { return VirtualPoint + new Size(0, h); } }

        public Point VirtualRight { get { return VirtualPoint + new Size(h, 0); } }

        public Point VirtualBottom { get { return VirtualPoint + new Size(0, -1 * h); } }

        public Point PointLeft { get { return Vis.GetLocation(VirtualLeft); } }

        public Point PointTop { get { return Vis.GetLocation(VirtualTop); } }

        public Point PointRight { get { return Vis.GetLocation(VirtualRight); } }

        public Point PointBottom { get { return Vis.GetLocation(VirtualBottom); } }

        public override bool ClickRange(Point cursor)
        {
            double x0 = 0;
            double x1 = 0;

            double[,] matrix = new double[2, 3]
                               {
                                   {PointTop.X - PointLeft.X, PointBottom.X - PointLeft.X, cursor.X - PointLeft.X},
                                   {PointTop.Y - PointLeft.Y, PointBottom.Y - PointLeft.Y, cursor.Y - PointLeft.Y}
                               };
            double k0 = matrix[1, 0]/matrix[0, 0]*-1;
            
            matrix[1, 0] += (matrix[0, 0] * k0);
            matrix[1, 1] += (matrix[0, 1] * k0);
            matrix[1, 2] += (matrix[0, 2] * k0);

            if (Math.Abs(matrix[1, 0]) > 0.001) return false;
            x1 = matrix[1, 2]/matrix[1, 1];
            x0 = (matrix[0, 2] - x1*matrix[0, 1])/matrix[0, 0];

            return (0 < x0 && x0 < 1) && (0 < x1 && x1 < 1);
        }

        public override void Draw(Graphics layer)
        {
            Pen p = new Pen(ColorRange);
            layer.DrawLine(p, PointLeft, PointTop);
            layer.DrawLine(p, PointTop, PointRight);
            layer.DrawLine(p, PointRight, PointBottom);
            layer.DrawLine(p, PointBottom, PointLeft);
            layer.FillPolygon(ColorRange, new Point[4] { PointLeft, PointTop, PointRight, PointBottom });
        }

        public event EventHandler<ShiftConnection> ShiftDiamodEvent;

        public override bool Shift(Size distance)
        {
            Point old = VirtualPoint;
            VirtualPoint += distance;
            if (ShiftDiamodEvent != null)
                ShiftDiamodEvent(this, new ShiftConnection(old, VirtualPoint));
            return true;
        }

        public override bool RemoveRectange()
        {
            if (Ovner != null) Ovner.RemoveDiamond(this);
            return base.RemoveRectange();
        }
    }
}
